/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

namespace Xceed.Silverlight.Data
{
  internal class SortPosition : SourcePosition
  {
    internal SortPosition(
      IList<SortDescription> sorting,
      IList<object> sortValues,
      StartPosition startingPosition )
    {
      if( sorting == null )
        throw new ArgumentNullException( "sorting" );

      if( sortValues == null )
        throw new ArgumentNullException( "sortValues" );

      if( sorting.Count < sortValues.Count )
        throw new InvalidOperationException( "The specified sortValues are not consistent with sorting." );

      m_sorting = new ReadOnlyCollection<SortDescription>( sorting );
      m_sortValues = new ReadOnlyCollection<object>( sortValues );
      m_startingPosition = startingPosition;
    }


    #region StartPosition Property

    internal StartPosition StartPosition
    {
      get
      {
        return m_startingPosition;
      }
    }

    private StartPosition m_startingPosition;

    #endregion StartPosition Property

    #region SortingReference Property

    internal IList<SortDescription> SortingReference
    {
      get
      {
        return m_sorting;
      }
    }

    private ReadOnlyCollection<SortDescription> m_sorting; //null

    #endregion SortingReference Property

    #region SortValues Property

    internal IList<object> SortValues
    {
      get
      {
        return m_sortValues;
      }
    }

    private ReadOnlyCollection<object> m_sortValues; //null

    #endregion SortValues Property


#if DEBUG
    public override string ToString()
    {
      System.Text.StringBuilder sb = new System.Text.StringBuilder( "SortPosition(" );
      for( int i=0;i < m_sortValues.Count;i++ )
      {
        if(i > 0)
        {
          sb.Append(", ");
        }
        object value = m_sortValues[ i ];
        sb.Append( value != null ? value.ToString() : "(null)" );
      }
      sb.Append( ")" );
      return sb.ToString();
    }
#endif

    internal static SortPosition Create(
      IEnumerable<SortDescription> sorting,
      RawDataItem dataItem,
      StartPosition startPosition )
    {
      if( sorting == null )
        throw new ArgumentNullException( "sorting" );

      if( !sorting.Any() )
        throw new ArgumentException( "At least one sort description must be specified.", "sorting" );

      if( dataItem == null )
        throw new ArgumentNullException( "dataItem" );

      IList<object> sortValues = ( from sort in sorting
                                   select dataItem.GetDataMemberValue( sort.PropertyName ) ).ToList();

      IList<SortDescription> sortDescriptions = sorting as IList<SortDescription>;
      if( sortDescriptions == null )
      {
        sortDescriptions = sorting.ToList();
      }

      return new SortPosition( sortDescriptions, sortValues, startPosition );
    }
  }
}
